from django.contrib import admin

# Register your models here.
from jobs.models import Job,Resume

# 定义列表的展现
class JobAdmin(admin.ModelAdmin):
    # 2. 虽然通过1我们可以完成了任务，但是我为了优化体验，因为当前登录的人就是创建人，时间我就默认取当前的，我不想选择，而且这三个选项我都不想看到
    # 2.1 exclude这是一个隐藏选项的方法,他接收的也是字符串
    exclude = ('creator','created_date','modified_date')
    # 1. list_display它是一个包含要显示的字段名的元组,list_display他接受的是str字符串，
    list_display = ('job_type','job_name','job_city','creator','created_date','modified_date')
    # 2.2 创建我们保存模型之前，进行选择我们当前登录人
    def save_model(self, request, obj, form, change):
        # 2.2.1获取我们的当前用户信息
        obj.creator=request.user
        # 2.2.2调用父类方法，进行保存。
        super().save_model(request,obj,form,change)
from django.contrib import messages
from interview.models import Candidate
from datetime import datetime

def enter_interview_process(modeladmin,request,queryset):
    candidate_names=""
    for obj in queryset:
        candidate=Candidate()
        candidate.__dict__.update(obj.__dict__)
        candidate.created_date=datetime.now()
        candidate.modified_date=datetime.now()
        candidate_names=candidate.username+","+candidate_names
        candidate.creator=request.user.username
        candidate.save()
    messages.add_message(request,messages.INFO,f"候选人{candidate_names}已经进入面试流程")
enter_interview_process.short_description = '进入面试流程'

class ResumeAdmin(admin.ModelAdmin):
    actions = (enter_interview_process,)
    list_display = ('username','applicant','city','apply_position','training_school','bachelor_school','master_school'
                    ,'doctor_school','major','created_date')
    readonly_fields = ('applicant','created_date','modified_date',)
    fieldsets = (
        (None,{'fields':(
            'applicant',('username'),('city'),('phone'),
            ('email'),('apply_position'),('born_address'),('gender',),
            ('training_school'),('bachelor_school'),('master_school'),('doctor_school'),('major'),('degree'),
            ('created_date'),('modified_date'),
            'candidate_introduction','work_experience','project_experience')}),
    )
    def save_model(self, request, obj, form, change):
        obj.applicant=request.user
        super().save_model(request,obj,form,change)

admin.site.register(Job,JobAdmin)
admin.site.register(Resume,ResumeAdmin)
